package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;

import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

    /**
     角色管理
     */

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    //查询所有角色
    @RequestMapping("/findAllRoles")
    public Result findAllRoles() {
        try {
            List<Role> roles = roleService.findAllRoles();
            return new Result(true, MessageConstant.GET_USER_ROLES_SUCCESS, roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USER_ROLES_FAIL);

        }
    }
    //角色分页查询
    @RequestMapping("/queryPage")
    public PageResult queryPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult page = roleService.queryPage(queryPageBean);
        return page;

    }
    //根据ID删除角色
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            roleService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);

    }

    //编辑角色,并更正其与权限对应的关系
    @RequestMapping("/edit")
    public Result edit(Integer[] permissionIds, @RequestBody Role role) {
        try {
            roleService.edit(permissionIds, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_PERMISSION_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_ROLE_PERMISSION_SUCCESS);

    }
    //根据ID查询对应的角色信息
    @RequestMapping("/findByRoleid")
    public Result findByRoleId(Integer id) {
        try {
            Role role = roleService.findByRoleId(id);
            return new Result(true, MessageConstant.GET_USER_ROLES_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USER_ROLES_FAIL);
        }

    }

    /**
     * 查询所有角色信息
     */
    @RequestMapping("/findAllRole")
    public Result findAllRole() {
        try {
            List<Role> roles = roleService.findAllRole();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    //新增角色,并可以添加权限
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds) {
        try {
            roleService.add(role, permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);//新增失败
        }
        return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);//新增成功
    }

}
